package com.nursinghome.management.controller;

import com.nursinghome.management.model.*;
import com.nursinghome.management.service.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/elderly")
public class ElderlyController {

    private final UserService userService;
    private final ElderlyService elderlyService;
    private final HealthRecordService healthRecordService;
    private final MedicationService medicationService;

    public ElderlyController(UserService userService, ElderlyService elderlyService,
                             HealthRecordService healthRecordService, MedicationService medicationService) {
        this.userService = userService;
        this.elderlyService = elderlyService;
        this.healthRecordService = healthRecordService;
        this.medicationService = medicationService;
    }

    @GetMapping("/dashboard")
    public String dashboard(Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String username = auth.getName();

        // Get current elderly
        User user = userService.loadUserByUsername(username);
        Elderly elderly = elderlyService.getElderlyByUser(user);

        // Get health records and medications
        List<HealthRecord> healthRecords = healthRecordService.getHealthRecordsByElderly(elderly);
        List<Medication> medications = medicationService.getActiveMedicationsByElderly(elderly);

        model.addAttribute("elderly", elderly);
        model.addAttribute("healthRecords", healthRecords);
        model.addAttribute("medications", medications);

        return "elderly/dashboard";
    }

    @GetMapping("/health-records")
    public String viewHealthRecords(Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String username = auth.getName();

        User user = userService.loadUserByUsername(username);
        Elderly elderly = elderlyService.getElderlyByUser(user);
        List<HealthRecord> healthRecords = healthRecordService.getHealthRecordsByElderly(elderly);

        model.addAttribute("elderly", elderly);
        model.addAttribute("healthRecords", healthRecords);

        return "elderly/health-records";
    }

    @GetMapping("/medications")
    public String viewMedications(Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String username = auth.getName();

        User user = userService.loadUserByUsername(username);
        Elderly elderly = elderlyService.getElderlyByUser(user);
        List<Medication> medications = medicationService.getActiveMedicationsByElderly(elderly);

        model.addAttribute("elderly", elderly);
        model.addAttribute("medications", medications);

        return "elderly/medications";
    }

    @GetMapping("/profile")
    public String profile(Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String username = auth.getName();

        User user = userService.loadUserByUsername(username);
        Elderly elderly = elderlyService.getElderlyByUser(user);

        model.addAttribute("user", user);
        model.addAttribute("elderly", elderly);

        return "elderly/profile";
    }
}